﻿using Microsoft.AspNetCore.Mvc;
using OpenIddict.Abstractions;
using Simple.Admin.System.Enums;
using Simple.Admin.System.Identity;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Identity;
using Volo.Abp.OpenIddict.Controllers;
using Volo.Abp.Security.Claims;

namespace Simple.Admin.Controller;

[Dependency(ReplaceServices = true)]
[ExposeServices(typeof(UserInfoController))]
[Route("connect/userinfo")]
[ApiExplorerSettings(IgnoreApi = true)]
public class SimpleAdminUserInfoController : UserInfoController
{
    protected override async Task<Dictionary<string, object>> GetUserInfoClaims()
    {
        IdentityUser? user = await UserManager.GetUserAsync(User);
        if (user == null)
        {
            return new Dictionary<string, object>();
        }

        Dictionary<string, object> claims = new(StringComparer.Ordinal)
        {
            [OpenIddictConstants.Claims.Subject] = await UserManager.GetUserIdAsync(user)
        };

        if (User.HasScope(OpenIddictConstants.Scopes.Profile))
        {
            claims[AbpClaimTypes.TenantId] = user.TenantId!;
            claims[OpenIddictConstants.Claims.PreferredUsername] = user.UserName;
            claims[OpenIddictConstants.Claims.FamilyName] = user.Surname;
            claims[OpenIddictConstants.Claims.GivenName] = user.Name;
            claims["avatar"] = user.GetProperty<string>(IdentityUserExtensionPropertyConst.User.AvatarUrl) ?? string.Empty;
            claims["surname"] = user.Surname;
            claims["userId"] = user.Id.ToString();
            claims["registrationDate"] = user.CreationTime.ToString("yyyy-MM-dd");
            claims["province"] = user.GetProperty<string>(IdentityUserExtensionPropertyConst.User.Province) ?? string.Empty;
            claims["city"] = user.GetProperty<string>(IdentityUserExtensionPropertyConst.User.City) ?? string.Empty;
            claims["district"] = user.GetProperty<string>(IdentityUserExtensionPropertyConst.User.District) ?? string.Empty;
            claims["address"] = user.GetProperty<string>(IdentityUserExtensionPropertyConst.User.Address) ?? string.Empty;
            claims["description"] = user.GetProperty<string>(IdentityUserExtensionPropertyConst.User.Introduction) ?? string.Empty;
            claims["sex"] = user.GetProperty<GenderEnum>(IdentityUserExtensionPropertyConst.User.Gender);
        }

        if (User.HasScope(OpenIddictConstants.Scopes.Email))
        {
            claims[OpenIddictConstants.Claims.Email] = await UserManager.GetEmailAsync(user) ?? string.Empty;
            claims[OpenIddictConstants.Claims.EmailVerified] = await UserManager.IsEmailConfirmedAsync(user);
        }

        if (User.HasScope(OpenIddictConstants.Scopes.Phone))
        {
            claims[OpenIddictConstants.Claims.PhoneNumber] =
                await UserManager.GetPhoneNumberAsync(user) ?? string.Empty;
            claims[OpenIddictConstants.Claims.PhoneNumberVerified] =
                await UserManager.IsPhoneNumberConfirmedAsync(user);
        }

        if (User.HasScope(OpenIddictConstants.Scopes.Roles))
        {
            claims[OpenIddictConstants.Claims.Role] = await UserManager.GetRolesAsync(user);
        }

        return claims;
    }
}
